package com.ceuer.demo.juc.forkjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class ForkJoinTest {
	public static void main(String[] args) throws ExecutionException, InterruptedException {
		test3();
	}
	
	//普通程序员 sum=500000000500000000 时间:10052
	public static void test1() {
		Long sum = 0L;
		long start = System.currentTimeMillis();
		for (Long i = 1L; i <= 10_0000_0000; i++) {
			sum += i;
		}
		long end = System.currentTimeMillis();
		System.out.println("sum=" + sum + " 时间:" + (end - start));
	}
	
	//ForkJoin sum=500000000500000000 时间:6695
	public static void test2() throws ExecutionException, InterruptedException {
		long start = System.currentTimeMillis();
		ForkJoinPool forkJoinPool = new ForkJoinPool();
		ForkJoinTask<Long> task = new ForkJoinDemo(0L, 10_0000_0000L);
		ForkJoinTask<Long> submit = forkJoinPool.submit(task);// 提交任务
		Long sum = submit.get();
		long end = System.currentTimeMillis();
		System.out.println("sum=" + sum + " 时间:" + (end - start));
	}
	
	//Stream并行流 sum=500000000500000000 时间:585
	public static void test3() {
		long start = System.currentTimeMillis();
		// Stream并行流
		long result = LongStream.rangeClosed(0L, 10_0000_0000L).parallel().reduce(0, Long::sum);
		long end = System.currentTimeMillis();
		System.out.println("result=" + result + " 时间:" + (end - start));
	}
}
